module.exports = {
  schedule: {
    cron: '0 0 3 * * ?',
    type: 'worker',
    env: ['pachong'],
    // immediate: true
  },

  async task(ctx) {
    let users = await ctx.model.User.find();

    for (let i = 0; i < users.length; i++) {
      let user = users[i];

      let topic_count = await ctx.model.Topic.count({
        tab: {$ne: 'share'},
        is_public: true,
        author_id: user.id
      });

      try {
        let result = await ctx.model.User.updateOne({_id: user.id}, {
          $set: {
            topic_count: topic_count
          },
        });
        console.log(result, i, users.length);
      } catch (e) {
        console.error(e.message);
      }
    }
  }
};
